package com.pauldada.sakurabbs.controllers;


import com.pauldada.sakurabbs.dao.PostRepository;
import com.pauldada.sakurabbs.dao.TypeRepository;
import com.pauldada.sakurabbs.entities.Post;
import com.pauldada.sakurabbs.entities.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("types")
public class TypeController {

    @Autowired
    TypeRepository typeRepository;

    @Autowired
    PostRepository postRepository;

    @GetMapping
    public String DisplayType(Model model){
        List<Type> types = typeRepository.findAll();
        model.addAttribute("types",types);
        return "types/list-types";
    }

    @GetMapping("/new")
    public String DisplayTypeForm(Model model){
        Type aType = new Type();
        List<Post> posts = postRepository.findAll();
        model.addAttribute("type",aType);
        model.addAttribute("allPost",posts);
        return "types/new-type";
    }

    @PostMapping("/save")
    public String CreateType(Model model, Type type){
        typeRepository.save(type);
        return "types/list-types";
    }
}
